package com.hainatong.shiphelper.module.warehouse.adapter;


import org.jetbrains.annotations.NotNull;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.module.warehouse.entity.WareShelfBean;

import android.view.View;

import java.util.List;

public class ShelfProvider extends BaseNodeProvider
{
    @Override
    public int getItemViewType()
    {
        return 1;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.item_shelf_child_view;
    }

    @Override
    public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode)
    {
        WareShelfBean entity = (WareShelfBean) baseNode;

        baseViewHolder.setText(R.id.tv_shelf_name, entity.getShelfName());
        baseViewHolder.setText(R.id.tv_count, entity.getFloors());
        //baseViewHolder.setImageResource( ,R.mipmap.ic_launcher)

         /*
        if (entity.isExpanded())
        {
            holder.setImageResource(R.id.iv, R.mipmap.ic_launcher);
        }
        else
        {
            holder.setImageResource(R.id.iv, R.mipmap.ic_launcher);
        }
        */

        setArrowSpin(baseViewHolder, baseNode, false);

    }


    @Override
    public void convert(@NotNull BaseViewHolder helper, @NotNull BaseNode data, @NotNull List<?> payloads)
    {
        for (Object payload : payloads) {
            if (payload instanceof Integer && (int) payload == ShelfAdapter.EXPAND_COLLAPSE_PAYLOAD) {
                // 增量刷新，使用动画变化箭头
                setArrowSpin(helper, data, true);
            }
        }
    }

    private void setArrowSpin(BaseViewHolder helper, BaseNode data, boolean isAnimate) {
        WareShelfBean entity = (WareShelfBean) data;
        // 箭头
        /*
        ImageView imageView = helper.getView(R.id.iv);
        if (entity.isExpanded()) {
            if (isAnimate) {
                ViewCompat.animate(imageView).setDuration(200)
                        .setInterpolator(new DecelerateInterpolator())
                        .rotation(0f)
                        .start();
            } else {
                imageView.setRotation(0f);
            }
        } else {
            if (isAnimate) {
                ViewCompat.animate(imageView).setDuration(200)
                        .setInterpolator(new DecelerateInterpolator())
                        .rotation(90f)
                        .start();
            } else {
                imageView.setRotation(90f);
            }
        }
        */
    }

    @Override
    public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
        // 这里使用payload进行增量刷新（避免整个item刷新导致的闪烁，不自然）
        getAdapter().expandOrCollapse(position, true, true, ShelfAdapter.EXPAND_COLLAPSE_PAYLOAD);
    }


}
